﻿<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"></meta><style>/*<![CDATA[*/

table{border: 1px solid gray;}
td{border: 1px dotted gray;}
p{margin: 3px 0 3px 0; padding: 0;}
#ID_Footer{font-size: small; font-style: italic; text-align: right; margin-top: 4em; padding-top: 4px; border-top: 2px solid gray;}

/*]]>*/</style><title>第5章 代码重用与函数编写</title></head><body>
<div><span style="font-family: 微软雅黑; font-size: 18pt; font-weight: bold; padding-left: 49pt; line-height: 190%">第</span><span style="font-family: Consolas; font-size: 18pt; font-weight: bold; padding-left: 49pt; line-height: 190%">5</span><span style="font-family: 微软雅黑; font-size: 18pt; font-weight: bold; padding-left: 49pt; line-height: 190%">章 &nbsp; &nbsp; &nbsp; &nbsp;代码重用与函数编写</span></div>
<div><span style="font-family: 微软雅黑; font-size: 16pt; padding-left: 70pt; line-height: 170%">5.1 &nbsp; &nbsp; &nbsp; &nbsp;</span><span style="font-family: 宋体; font-size: 16pt; padding-left: 70pt; line-height: 170%">使用</span><span style="font-family: Calibri; font-size: 16pt; padding-left: 70pt; line-height: 170%">require()</span><span style="font-family: 宋体; font-size: 16pt; padding-left: 70pt; line-height: 170%">和</span><span style="font-family: Calibri; font-size: 16pt; padding-left: 70pt; line-height: 170%">include()</span><span style="font-family: 宋体; font-size: 16pt; padding-left: 70pt; line-height: 170%">函数</span><span style="font-family: Calibri; font-size: 16pt; padding-left: 70pt; line-height: 170%">(p97)</span></div>
<div><span style="font-family: Consolas; font-size: 12pt; color: #ff0000; line-height: 170%">require</span><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">和</span><span style="font-family: Consolas; font-size: 12pt; font-weight: bold; color: #ff0000; line-height: 170%">include</span><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">可将文件载入到</span><span style="font-family: Consolas; font-size: 12pt; line-height: 170%">php</span><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">脚本中。</span></div>
<div><span style="font-family: 宋体; font-size: 10pt; font-weight: bold; padding-left: 40pt; line-height: 190%">* </span><span style="font-family: 宋体; font-size: 11pt; padding-left: 40pt; line-height: 190%">函数失败后，</span><span style="font-family: Consolas; font-size: 11pt; padding-left: 40pt; line-height: 190%">require</span><span style="font-family: 宋体; font-size: 11pt; padding-left: 40pt; line-height: 190%">给出一个</span><span style="font-family: 宋体; font-size: 11pt; font-weight: bold; padding-left: 40pt; line-height: 190%">致命错误</span><span style="font-family: 宋体; font-size: 11pt; padding-left: 40pt; line-height: 190%">，而</span><span style="font-family: Consolas; font-size: 11pt; padding-left: 40pt; line-height: 190%">include</span><span style="font-family: 宋体; font-size: 11pt; padding-left: 40pt; line-height: 190%">只给出一个</span><span style="font-family: 宋体; font-size: 11pt; font-weight: bold; padding-left: 40pt; line-height: 190%">警告</span></div>
<div><span style="font-family: Consolas; font-size: 12pt; font-weight: bold; color: #ff0000; line-height: 190%">require_once</span><span style="font-family: Consolas; font-size: 12pt; line-height: 190%">()</span><span style="font-family: 宋体; font-size: 12pt; line-height: 190%">和</span><span style="font-family: Consolas; font-size: 12pt; font-weight: bold; color: #ff0000; line-height: 190%">include_once</span><span style="font-family: Consolas; font-size: 12pt; line-height: 190%">()</span><span style="font-family: 宋体; font-size: 12pt; line-height: 190%">，确保一个包含的文件只能被引用一次。</span></div>
<div><span style="font-family: Consolas; font-size: 12pt; font-weight: bold; line-height: 240%">require</span><span style="font-family: 宋体; font-size: 12pt; line-height: 240%">和</span><span style="font-family: Consolas; font-size: 12pt; font-weight: bold; line-height: 240%">include</span><span style="font-family: 宋体; font-size: 12pt; line-height: 240%">速度会更快。</span></div>
<div><span style="font-family: Consolas; font-size: 12pt; font-weight: bold; color: #000000; line-height: 160%">5.1.1 &nbsp; &nbsp; &nbsp; &nbsp;</span><span style="font-family: 宋体; font-size: 12pt; font-weight: bold; color: #000000; line-height: 160%">文件扩展名和</span><span style="font-family: Consolas; font-size: 12pt; font-weight: bold; color: #000000; line-height: 160%">require()</span><span style="font-family: 宋体; font-size: 12pt; font-weight: bold; color: #000000; line-height: 160%">函数</span></div>
<div><span style="font-family: Consolas; font-size: 12pt; line-height: 190%">php</span><span style="font-family: 宋体; font-size: 12pt; font-weight: bold; line-height: 190%">不会查看所需文件的扩展名</span><span style="font-family: 宋体; font-size: 12pt; line-height: 190%">，使用</span><span style="font-family: Consolas; font-size: 12pt; line-height: 190%">require()</span><span style="font-family: 宋体; font-size: 12pt; line-height: 190%">载入文件时，它会作为</span><span style="font-family: Consolas; font-size: 12pt; line-height: 190%">php</span><span style="font-family: 宋体; font-size: 12pt; line-height: 190%">一部分执行。</span></div>
<div><span style="font-family: Consolas; font-size: 12pt; line-height: 190%">php</span><span style="font-family: 宋体; font-size: 12pt; line-height: 190%">语句放入</span><span style="font-family: Consolas; font-size: 12pt; line-height: 190%">html</span><span style="font-family: 宋体; font-size: 12pt; line-height: 190%">文件中时（扩展名为</span><span style="font-family: Consolas; font-size: 12pt; line-height: 190%">.html</span><span style="font-family: 宋体; font-size: 12pt; line-height: 190%">），是不会被处理的，但</span><span style="font-family: 宋体; font-size: 12pt; font-weight: bold; color: #800080; line-height: 190%">通过</span><span style="font-family: Consolas; font-size: 12pt; font-weight: bold; color: #800080; line-height: 190%">require</span><span style="font-family: 宋体; font-size: 12pt; font-weight: bold; color: #800080; line-height: 190%">语句载入</span><span style="font-family: Consolas; font-size: 12pt; font-weight: bold; color: #800080; line-height: 190%">*.html</span><span style="font-family: 宋体; font-size: 12pt; font-weight: bold; color: #800080; line-height: 190%">时</span><span style="font-family: 宋体; font-size: 12pt; line-height: 190%">，里头的</span><span style="font-family: Consolas; font-size: 12pt; line-height: 190%">php</span><span style="font-family: 宋体; font-size: 12pt; line-height: 190%">语句会被处理。（通常扩展名命名为</span><span style="font-family: Consolas; font-size: 12pt; line-height: 190%">*</span><span style="font-family: Consolas; font-size: 12pt; font-weight: bold; line-height: 190%">.inc</span><span style="font-family: 宋体; font-size: 12pt; line-height: 190%">或</span><span style="font-family: Consolas; font-size: 12pt; line-height: 190%">*</span><span style="font-family: Consolas; font-size: 12pt; font-weight: bold; line-height: 190%">.php</span><span style="font-family: 宋体; font-size: 12pt; line-height: 190%">）</span></div>
<div><span style="font-family: 宋体; font-size: 12pt; line-height: 240%">文件中的代码</span><span style="font-family: 宋体; font-size: 12pt; font-weight: bold; line-height: 240%">放入</span><span style="font-family: Consolas; font-size: 12pt; font-weight: bold; line-height: 240%">php</span><span style="font-family: 宋体; font-size: 12pt; font-weight: bold; line-height: 240%">标记之间</span><span style="font-family: 宋体; font-size: 12pt; line-height: 240%">，否则会被当成文件或</span><span style="font-family: Consolas; font-size: 12pt; line-height: 240%">html</span><span style="font-family: 宋体; font-size: 12pt; line-height: 240%">脚本而不被执行。</span></div>
<div><span style="font-family: Consolas; font-size: 12pt; font-weight: bold; color: #000000; line-height: 160%">5.1.2 &nbsp; &nbsp; &nbsp; &nbsp;</span><span style="font-family: 宋体; font-size: 12pt; font-weight: bold; color: #000000; line-height: 160%">使用</span><span style="font-family: Consolas; font-size: 12pt; font-weight: bold; color: #000000; line-height: 160%">require()</span><span style="font-family: 宋体; font-size: 12pt; font-weight: bold; color: #000000; line-height: 160%">制作</span><span style="font-family: Consolas; font-size: 12pt; font-weight: bold; color: #000000; line-height: 160%">Web</span><span style="font-family: 宋体; font-size: 12pt; font-weight: bold; color: #000000; line-height: 160%">站点的模板</span></div>
<div><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">可以在</span><span style="font-family: Consolas; font-size: 12pt; line-height: 170%">php</span><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">中使用</span><span style="font-family: Consolas; font-size: 12pt; line-height: 170%">require()</span><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">语句将</span><span style="font-family: 宋体; font-size: 12pt; font-weight: bold; line-height: 170%">模板</span><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">和</span><span style="font-family: 宋体; font-size: 12pt; font-weight: bold; line-height: 170%">标准元素</span><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">加入到页面中。</span></div>
<div><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">可将</span><span style="font-family: Consolas; font-size: 12pt; line-height: 170%">.inc</span><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">文件保存在一个目录中，这个目录可以被脚本访问，但被引入的文件不会被</span><span style="font-family: Consolas; font-size: 12pt; line-height: 170%">Web</span><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">服务器载入。</span><span style="font-family: Consolas; font-size: 12pt; line-height: 170%">(p101)</span></div>
<div><span style="font-family: Consolas; font-size: 12pt; font-weight: bold; color: #ff0000; line-height: 240%">readfile</span><span style="font-family: Consolas; font-size: 12pt; line-height: 240%">()</span><span style="font-family: 宋体; font-size: 12pt; line-height: 240%">替代方法，则文件被当成纯文本或</span><span style="font-family: Consolas; font-size: 12pt; line-height: 240%">HTML</span><span style="font-family: 宋体; font-size: 12pt; line-height: 240%">，不会执行</span><span style="font-family: Consolas; font-size: 12pt; line-height: 240%">php</span><span style="font-family: 宋体; font-size: 12pt; line-height: 240%">，</span></div>
<div><span style="font-family: Consolas; font-size: 12pt; font-weight: bold; color: #000000; line-height: 190%">5.1.3 &nbsp; &nbsp; &nbsp; &nbsp;auto_prepend_file</span><span style="font-family: 宋体; font-size: 12pt; font-weight: bold; color: #000000; line-height: 190%">和</span><span style="font-family: Consolas; font-size: 12pt; font-weight: bold; color: #000000; line-height: 190%">auto_append_file</span></div>
<div><span style="font-family: Consolas; font-size: 12pt; line-height: 170%">php.ini</span><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">中选项，</span><span style="font-family: Consolas; font-size: 12pt; font-weight: bold; color: #ff0000; line-height: 170%">auto_prepend_file</span><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">和</span><span style="font-family: Consolas; font-size: 12pt; font-weight: bold; color: #ff0000; line-height: 170%">auto_append_file</span></div>
<div><span style="font-family: 宋体; font-size: 10pt; font-weight: bold; padding-left: 40pt; line-height: 160%">* &nbsp; &nbsp; &nbsp; &nbsp;</span><span style="font-family: 宋体; font-size: 11pt; padding-left: 40pt; line-height: 160%">可用来设置页眉页脚，保证它们在每个页面的前后被载入。</span></div>
<div><span style="font-family: 宋体; font-size: 10pt; font-weight: bold; padding-left: 40pt; line-height: 240%">* &nbsp; &nbsp; &nbsp; &nbsp;</span><span style="font-family: 宋体; font-size: 11pt; padding-left: 40pt; line-height: 240%">同</span><span style="font-family: Consolas; font-size: 11pt; padding-left: 40pt; line-height: 240%">include()</span><span style="font-family: 宋体; font-size: 11pt; padding-left: 40pt; line-height: 240%">，页面不存在将产生</span><span style="font-family: 宋体; font-size: 11pt; font-weight: bold; padding-left: 40pt; line-height: 240%">警告</span><span style="font-family: 宋体; font-size: 11pt; padding-left: 40pt; line-height: 240%">。</span></div>
<div><span style="font-family: Consolas; font-size: 12pt; line-height: 170%">Apache</span><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">服务器允许对单个目录不同配置选项进行修改</span></div>
<div><span style="font-family: 宋体; font-size: 10pt; font-weight: bold; padding-left: 40pt; line-height: 160%">* &nbsp; &nbsp; &nbsp; &nbsp;</span><span style="font-family: 宋体; font-size: 11pt; padding-left: 40pt; line-height: 160%">服务器须允许设置其主配置文件</span></div>
<div><span style="font-family: 宋体; font-size: 10pt; font-weight: bold; padding-left: 40pt; line-height: 160%">* &nbsp; &nbsp; &nbsp; &nbsp;</span><span style="font-family: 宋体; font-size: 11pt; padding-left: 40pt; line-height: 160%">目录中创建</span><span style="font-family: Consolas; font-size: 11pt; font-weight: bold; color: #800080; padding-left: 40pt; line-height: 160%">.htaccess</span><span style="font-family: 宋体; font-size: 11pt; padding-left: 40pt; line-height: 160%">，包含两行</span></div>
<div><span style="font-family: Consolas; font-size: 11pt; font-style: italic; color: #c45911; padding-left: 21pt; line-height: 160%">php_value auto_prepend_file &quot;/home/username/include/header.php&quot;</span></div>
<div><span style="font-family: Consolas; font-size: 11pt; padding-left: 21pt; line-height: 240%">php_value auto_append_fiel &quot;/home/username/include/footer.php&quot;</span></div>
<div><br /></div>
<div><span style="font-family: 微软雅黑; font-size: 16pt; font-weight: bold; padding-left: 70pt; line-height: 170%">5.2 &nbsp; &nbsp; &nbsp; &nbsp;</span><span style="font-family: 宋体; font-size: 16pt; font-weight: bold; padding-left: 70pt; line-height: 170%">在</span><span style="font-family: Calibri; font-size: 16pt; font-weight: bold; padding-left: 70pt; line-height: 170%">php</span><span style="font-family: 宋体; font-size: 16pt; font-weight: bold; padding-left: 70pt; line-height: 170%">中使用函数</span></div>
<div><span style="font-family: Consolas; font-size: 12pt; color: #000000; line-height: 160%">5.2.1 &nbsp; &nbsp; &nbsp; &nbsp;</span><span style="font-family: 宋体; font-size: 12pt; color: #000000; line-height: 160%">调用函数</span></div>
<div><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">函数</span><span style="font-family: Consolas; font-size: 12pt; font-weight: bold; color: #ff0000; line-height: 170%">phpinfo()</span><span style="font-family: Times New Roman; font-size: 12pt; line-height: 170%">,</span><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">显示已安装的</span><span style="font-family: Consolas; font-size: 12pt; line-height: 170%">php</span><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">版本，关于</span><span style="font-family: Consolas; font-size: 12pt; line-height: 170%">php</span><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">的信息，</span><span style="font-family: Consolas; font-size: 12pt; line-height: 170%">Web</span><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">服务器的设置和众多的</span><span style="font-family: Consolas; font-size: 12pt; line-height: 170%">PHP</span><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">和服务器变量的值。</span></div>
<div><span style="font-family: 宋体; font-size: 12pt; font-weight: bold; line-height: 240%">函数原型</span><span style="font-family: 宋体; font-size: 12pt; line-height: 240%">：给出参数个数，每个参数表示的对象，及参数的数据类型。</span></div>
<div><span style="font-family: Consolas; font-size: 12pt; font-weight: bold; color: #000000; line-height: 160%">5.2.2 &nbsp; &nbsp; &nbsp; &nbsp;</span><span style="font-family: 宋体; font-size: 12pt; font-weight: bold; color: #000000; line-height: 160%">调用未定义的函数</span></div>
<div><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">调用并不存在的函数会得到错误信息</span></div>
<div><span style="font-family: 宋体; font-size: 10pt; font-weight: bold; padding-left: 40pt; line-height: 240%">* </span><span style="font-family: 宋体; font-size: 11pt; padding-left: 40pt; line-height: 240%">告诉我们错误在</span><span style="font-family: 宋体; font-size: 11pt; font-weight: bold; padding-left: 40pt; line-height: 240%">哪个文件</span><span style="font-family: 宋体; font-size: 11pt; padding-left: 40pt; line-height: 240%">，</span><span style="font-family: 宋体; font-size: 11pt; font-weight: bold; padding-left: 40pt; line-height: 240%">哪一行</span><span style="font-family: 宋体; font-size: 11pt; padding-left: 40pt; line-height: 240%">，及试图调用的</span><span style="font-family: 宋体; font-size: 11pt; font-weight: bold; padding-left: 40pt; line-height: 240%">函数的名称</span></div>
<div><span style="font-family: Consolas; font-size: 11pt; font-weight: bold; color: #000000; line-height: 160%">5.2.3 &nbsp; &nbsp; &nbsp; &nbsp;</span><span style="font-family: 宋体; font-size: 12pt; font-weight: bold; color: #000000; line-height: 160%">理解字母大小写和函数名称</span></div>
<div><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">函数调用</span><span style="font-family: 宋体; font-size: 12pt; font-weight: bold; line-height: 170%">不区分</span><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">大小写</span></div>
<div><span style="font-family: 宋体; font-size: 12pt; line-height: 240%">变量名区分大小写</span></div>
<div><br /></div>
<div><span style="font-family: 微软雅黑; font-size: 16pt; font-weight: bold; padding-left: 70pt; line-height: 170%">5.3 &nbsp; &nbsp; &nbsp; &nbsp;</span><span style="font-family: 宋体; font-size: 16pt; font-weight: bold; padding-left: 70pt; line-height: 170%">了解基本的函数结构（</span><span style="font-family: Calibri; font-size: 16pt; font-weight: bold; padding-left: 70pt; line-height: 170%">p106</span><span style="font-family: 宋体; font-size: 16pt; font-weight: bold; padding-left: 70pt; line-height: 170%">）</span></div>
<div><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">函数声明：创建或者声明一个新的函数</span></div>
<div><span style="font-family: 宋体; font-size: 10pt; font-weight: bold; padding-left: 40pt; line-height: 240%">* </span><span style="font-family: 宋体; font-size: 11pt; padding-left: 40pt; line-height: 240%">关键字</span><span style="font-family: Consolas; font-size: 11pt; font-weight: bold; color: #ff0000; padding-left: 40pt; line-height: 240%">function</span><span style="font-family: 宋体; font-size: 11pt; padding-left: 40pt; line-height: 240%">开始</span></div>
<div><span style="font-family: 宋体; font-size: 12pt; font-weight: bold; line-height: 240%">内置函数</span><span style="font-family: 宋体; font-size: 12pt; line-height: 240%">：所有</span><span style="font-family: Consolas; font-size: 12pt; line-height: 240%">php</span><span style="font-family: 宋体; font-size: 12pt; line-height: 240%">脚本中都可以使用</span></div>
<div><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">声明</span><span style="font-family: 宋体; font-size: 12pt; font-weight: bold; line-height: 170%">自定义函数</span><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">，只能在声明它的脚本中使用</span></div>
<div><span style="font-family: 宋体; font-size: 10pt; font-weight: bold; padding-left: 40pt; line-height: 240%">* </span><span style="font-family: 宋体; font-size: 11pt; padding-left: 40pt; line-height: 240%">可将常用函数包含到一个文件中，所有脚本中调用</span><span style="font-family: Consolas; font-size: 11pt; padding-left: 40pt; line-height: 240%">require()</span></div>
<div><span style="font-family: 宋体; font-size: 12pt; font-weight: bold; line-height: 170%">花括号</span><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">，函数中代码放入其中</span></div>
<div><span style="font-family: 宋体; font-size: 10pt; font-weight: bold; padding-left: 40pt; line-height: 160%">* </span><span style="font-family: 宋体; font-size: 11pt; padding-left: 40pt; line-height: 160%">可以包含任何</span><span style="font-family: Consolas; font-size: 11pt; padding-left: 40pt; line-height: 160%">php</span><span style="font-family: 宋体; font-size: 11pt; padding-left: 40pt; line-height: 160%">脚本的其他地方都合法的代码</span></div>
<div><span style="font-family: 宋体; font-size: 10pt; font-weight: bold; padding-left: 40pt; line-height: 240%">* </span><span style="font-family: 宋体; font-size: 11pt; padding-left: 40pt; line-height: 240%">包括函数调用，新变量，函数声明，</span><span style="font-family: Consolas; font-size: 11pt; padding-left: 40pt; line-height: 240%">require(), include</span><span style="font-family: 宋体; font-size: 11pt; padding-left: 40pt; line-height: 240%">语句类声明及</span><span style="font-family: Consolas; font-size: 11pt; padding-left: 40pt; line-height: 240%">html</span><span style="font-family: 宋体; font-size: 11pt; padding-left: 40pt; line-height: 240%">脚本</span></div>
<div><span style="font-family: 宋体; font-size: 12pt; font-weight: bold; line-height: 240%">函数命名</span><span style="font-family: 宋体; font-size: 12pt; line-height: 240%">同</span><span style="font-family: Consolas; font-size: 12pt; line-height: 240%">C/C++</span></div>
<div><span style="font-family: Consolas; font-size: 12pt; line-height: 170%">PHP</span><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">不支持函数重载（旧版，</span><span style="font-family: Consolas; font-size: 12pt; line-height: 170%">5</span><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">以前，见</span><span style="font-family: Consolas; font-size: 12pt; line-height: 170%">P107</span><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">）</span></div>
<div><span style="font-family: 宋体; font-size: 10pt; font-weight: bold; padding-left: 40pt; line-height: 160%">* </span><span style="font-family: 宋体; font-size: 11pt; padding-left: 40pt; line-height: 160%">不能和内置或用户已定义函数重名</span></div>
<div><span style="font-family: 宋体; font-size: 10pt; font-weight: bold; padding-left: 40pt; line-height: 240%">* </span><span style="font-family: 宋体; font-size: 11pt; padding-left: 40pt; line-height: 240%">每个脚本知道所有内置函数</span></div>
<div><span style="font-family: 宋体; font-size: 12pt; font-weight: bold; line-height: 170%">可变函数</span></div>
<div><span style="font-family: 宋体; font-size: 10pt; padding-left: 40pt; line-height: 240%">* </span><span style="font-family: Consolas; font-size: 11pt; padding-left: 40pt; line-height: 240%">$name();</span></div>
<div><br /></div>
<div><span style="font-family: 微软雅黑; font-size: 16pt; font-weight: bold; padding-left: 70pt; line-height: 170%">5.4 &nbsp; &nbsp; &nbsp; &nbsp;</span><span style="font-family: 宋体; font-size: 16pt; font-weight: bold; padding-left: 70pt; line-height: 170%">使用参数</span></div>
<div><span style="font-family: 宋体; font-size: 12pt; line-height: 240%">用户定义函数可以有多个参数和可选参数（即可给定默认值）。</span></div>
<div><span style="font-family: 宋体; font-size: 12pt; line-height: 240%">不能漏掉一个可选参数而给出参数列表中的后一个参数</span></div>
<div><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">可以声明能够接收可变参数数量的函数（</span><span style="font-family: Consolas; font-size: 12pt; line-height: 170%">p109-p110</span><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">）</span></div>
<div><span style="font-family: 宋体; font-size: 10pt; font-weight: bold; padding-left: 40pt; line-height: 240%">* </span><span style="font-family: 宋体; font-size: 11pt; padding-left: 40pt; line-height: 240%">通过</span><span style="font-family: Consolas; font-size: 11pt; padding-left: 40pt; line-height: 240%">3</span><span style="font-family: 宋体; font-size: 11pt; padding-left: 40pt; line-height: 240%">个帮助器函数：</span><span style="font-family: Consolas; font-size: 11pt; font-weight: bold; padding-left: 40pt; line-height: 240%">func_num_args()</span><span style="font-family: Consolas; font-size: 11pt; padding-left: 40pt; line-height: 240%">, </span><span style="font-family: Consolas; font-size: 11pt; font-weight: bold; padding-left: 40pt; line-height: 240%">func_get_arg()</span><span style="font-family: Consolas; font-size: 11pt; padding-left: 40pt; line-height: 240%">, </span><span style="font-family: Consolas; font-size: 11pt; font-weight: bold; padding-left: 40pt; line-height: 240%">func_get_args()</span><span style="font-family: 宋体; font-size: 11pt; padding-left: 40pt; line-height: 240%">可以确定已经传递了多少个参数以及这些参数的值。</span></div>
<div><br /></div>
<div><span style="font-family: 微软雅黑; font-size: 16pt; font-weight: bold; padding-left: 70pt; line-height: 170%">5.5 &nbsp; &nbsp; &nbsp; &nbsp;</span><span style="font-family: 宋体; font-size: 16pt; font-weight: bold; padding-left: 70pt; line-height: 170%">理解作用域</span></div>
<div><span style="font-family: 宋体; font-size: 12pt; line-height: 240%">函数内声明的变量，函数作用域，称为</span><span style="font-family: 宋体; font-size: 12pt; font-weight: bold; color: #ff0000; line-height: 240%">局部变量</span></div>
<div><span style="font-family: 宋体; font-size: 12pt; line-height: 240%">在函数外部声明的变量，作用域从声明处到文件末尾（不包含函数内部），称为</span><span style="font-family: 宋体; font-size: 12pt; font-weight: bold; color: #ff0000; line-height: 240%">全局变量。</span></div>
<div><span style="font-family: 宋体; font-size: 12pt; font-weight: bold; color: #ff0000; line-height: 240%">超级全局变量</span><span style="font-family: 宋体; font-size: 12pt; line-height: 240%">，函数内外部都可见</span></div>
<div><span style="font-family: Consolas; font-size: 12pt; line-height: 240%">require()</span><span style="font-family: 宋体; font-size: 12pt; line-height: 240%">和</span><span style="font-family: Consolas; font-size: 12pt; line-height: 240%">include()</span><span style="font-family: 宋体; font-size: 12pt; line-height: 240%">不影响作用域，若这两个语句在函数内部，则函数作用域，否则全局适用</span></div>
<div><span style="font-family: 宋体; font-size: 12pt; line-height: 240%">关键字</span><span style="font-family: Consolas; font-size: 12pt; line-height: 240%">&quot;</span><span style="font-family: Consolas; font-size: 12pt; font-weight: bold; color: #ff0000; line-height: 240%">global</span><span style="font-family: Consolas; font-size: 12pt; line-height: 240%">&quot;</span><span style="font-family: 宋体; font-size: 12pt; line-height: 240%">，用来手动指定一个在</span><span style="font-family: 宋体; font-size: 12pt; font-weight: bold; line-height: 240%">函数中定义</span><span style="font-family: 宋体; font-size: 12pt; line-height: 240%">或使用的变量具有全局作用域。</span></div>
<div><span style="font-family: Consolas; font-size: 12pt; font-weight: bold; color: #ff0000; line-height: 240%">unset</span><span style="font-family: Consolas; font-size: 12pt; line-height: 240%">($variable_name)</span><span style="font-family: 宋体; font-size: 12pt; line-height: 240%">可手动删除变量</span></div>
<div><br /></div>
<div><span style="font-family: 微软雅黑; font-size: 16pt; font-weight: bold; padding-left: 70pt; line-height: 170%">5.6 &nbsp; &nbsp; &nbsp; &nbsp;</span><span style="font-family: 宋体; font-size: 16pt; font-weight: bold; padding-left: 70pt; line-height: 170%">引用传递和值传递</span></div>
<div><span style="font-family: 宋体; font-size: 12pt; line-height: 240%">引用传递</span><span style="font-family: 宋体; font-size: 12pt; line-height: 240%">，在函数定义的参数名前加上</span><span style="font-family: 宋体; font-size: 12pt; font-weight: bold; color: #ff0000; line-height: 240%">地址符（</span><span style="font-family: Consolas; font-size: 12pt; font-weight: bold; color: #ff0000; line-height: 240%">&amp;</span><span style="font-family: 宋体; font-size: 12pt; font-weight: bold; color: #ff0000; line-height: 240%">）</span></div>
<div><br /></div>
<div><span style="font-family: 微软雅黑; font-size: 16pt; padding-left: 70pt; line-height: 170%">5.7 &nbsp; &nbsp; &nbsp; &nbsp;</span><span style="font-family: 宋体; font-size: 16pt; padding-left: 70pt; line-height: 170%">使用</span><span style="font-family: Calibri; font-size: 16pt; padding-left: 70pt; line-height: 170%">Return</span><span style="font-family: 宋体; font-size: 16pt; padding-left: 70pt; line-height: 170%">关键字</span></div>
<div><span style="font-family: Consolas; font-size: 12pt; color: #ff0000; line-height: 190%">return</span><span style="font-family: Consolas; font-size: 12pt; color: #ff0000; line-height: 190%"> </span><span style="font-family: 宋体; font-size: 12pt; line-height: 190%">终止函数的执行，可以返回一个值（</span><span style="font-family: Consolas; font-size: 12pt; line-height: 190%">p114</span><span style="font-family: 宋体; font-size: 12pt; line-height: 190%">）</span></div>
<div><span style="font-family: 宋体; font-size: 12pt; line-height: 240%">内置函数</span><span style="font-family: Consolas; font-size: 12pt; font-weight: bold; color: #ff0000; line-height: 240%">isset</span><span style="font-family: Consolas; font-size: 12pt; line-height: 240%">()</span><span style="font-family: 宋体; font-size: 12pt; line-height: 240%">，判断一个变量是否已经创建并赋值了</span></div>
<div><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">内置函数</span><span style="font-family: Consolas; font-size: 12pt; font-weight: bold; color: #ff0000; line-height: 170%">max()</span></div>
<div><span style="font-family: 宋体; font-size: 10pt; font-weight: bold; padding-left: 40pt; line-height: 160%">* &nbsp; &nbsp; &nbsp; &nbsp;</span><span style="font-family: 宋体; font-size: 11pt; padding-left: 40pt; line-height: 160%">返回两个参数中较大的一个</span></div>
<div><span style="font-family: 宋体; font-size: 10pt; font-weight: bold; padding-left: 40pt; line-height: 160%">* &nbsp; &nbsp; &nbsp; &nbsp;</span><span style="font-family: 宋体; font-size: 11pt; padding-left: 40pt; line-height: 160%">若两个都没赋值，则不会返回任何东西</span></div>
<div><span style="font-family: 宋体; font-size: 10pt; font-weight: bold; padding-left: 40pt; line-height: 240%">* &nbsp; &nbsp; &nbsp; &nbsp;</span><span style="font-family: 宋体; font-size: 11pt; padding-left: 40pt; line-height: 240%">其中一个赋值，另一个未赋值，则返回赋值的那个</span></div>
<div><span style="font-family: 宋体; font-size: 12pt; font-weight: bold; color: #ff0000; line-height: 170%">名称空间</span><span style="font-family: Consolas; font-size: 12pt; line-height: 170%">(p116)</span></div>
<div><span style="font-family: 宋体; font-size: 10pt; font-weight: bold; padding-left: 40pt; line-height: 160%">* &nbsp; &nbsp; &nbsp; &nbsp;</span><span style="font-family: 宋体; font-size: 11pt; padding-left: 40pt; line-height: 160%">是一个抽象的</span><span style="font-family: 宋体; font-size: 11pt; font-weight: bold; padding-left: 40pt; line-height: 160%">容器</span><span style="font-family: 宋体; font-size: 11pt; padding-left: 40pt; line-height: 160%">，可以包含函数、常量及类的定义</span></div>
<div><span style="font-family: 宋体; font-size: 10pt; font-weight: bold; padding-left: 40pt; line-height: 160%">* &nbsp; &nbsp; &nbsp; &nbsp;</span><span style="font-family: 宋体; font-size: 11pt; padding-left: 40pt; line-height: 160%">其中的所有函数、类和常量都将自动冠以名称</span><span style="font-family: 宋体; font-size: 11pt; font-weight: bold; padding-left: 40pt; line-height: 160%">空间前缀</span></div>
<div><span style="font-family: 宋体; font-size: 10pt; font-weight: bold; padding-left: 40pt; line-height: 160%">* &nbsp; &nbsp; &nbsp; &nbsp;</span><span style="font-family: 宋体; font-size: 11pt; padding-left: 40pt; line-height: 160%">非全路径的类、函数和常量名称将在运行时解析，在查看</span><span style="font-family: 宋体; font-size: 11pt; font-weight: bold; padding-left: 40pt; line-height: 160%">全局空间</span><span style="font-family: 宋体; font-size: 11pt; padding-left: 40pt; line-height: 160%">之前，先查看名称空间。</span></div>
<div><br /></div>
<div><br /></div>
<div><br /></div>
<div><br /></div>
<div><br /></div>
<div><br /></div>
<div><br /></div>
<div><br /></div>
<div><br /></div>
<div><br /></div>
<div><br /></div>
<div><br /></div>
<div><br /></div>
<div><br /></div>
<div><br /></div>
<div><br /></div>
<div><br /></div>
<div><span style="font-family: Times New Roman; font-size: 12pt">
</span></div>
<div><br /></div><script type="text/javascript" language="javascript" src="jquery.js"></script><script type="text/javascript" language="javascript" src="itemlink.js"></script></body></html>